package com.dianrui.agent.activity;


import android.widget.ListView;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.adapter.FixAdapter;
import com.dianrui.agent.bean.Area;
import com.dianrui.agent.event.ChooseNetwork;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ChooseNetWorkActivity extends BaseActivity {
    private List<Area> areaList = new ArrayList<>();
    private FixAdapter fixAdapter;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.listview)
    ListView listview;


    @Override
    public int getContentViewId() {
        return R.layout.activity_networklist;
    }

    @Override
    public void initView() {
        title.setText("选择区域");
        fixAdapter = new FixAdapter(ChooseNetWorkActivity.this, areaList);
        listview.setAdapter(fixAdapter);
        getArea();
        fixAdapter.setOnAreaClickListener(new FixAdapter.AreaClickListener() {
            @Override
            public void onAreaClick(String areaId) {
                SharedUtil.setAreaid(areaId);
                EventBus.getDefault().post(new ChooseNetwork(areaId));
                finish();
            }
        });


    }

    private void getArea() {
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl() + Url.GETAREA, "", new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result) && !isFinishing()) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if ("1000".equals(jsonObject.optString("status"))) {
                            JSONArray data = jsonObject.optJSONArray("data");
                            if (data.length() > 0) {
                                for (int i = 0; i < data.length(); i++) {
                                    Area area = new Area();
                                    area.area_id = data.optJSONObject(i).optString("area_id");
                                    area.area_name = data.optJSONObject(i).optString("area_name");
                                    area.remark = data.optJSONObject(i).optString("remark");
                                    area.is_checked = data.optJSONObject(i).optString("is_checked");
                                    areaList.add(area);
                                }
                                if (fixAdapter != null) {
                                    fixAdapter.refresh(areaList);
                                }

                            }
                        } else if ("1001".equals(jsonObject.optString("status"))) {
                            ToastUtil.showToast(jsonObject.optString("message"));
                        } else if ("1002".equals(jsonObject.optString("status"))) {
                            spUtils.clear();
                            ToastUtil.showToast(jsonObject.optString("message"));
                            JumpActivitys(LoginActivity.class);
                            finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
